/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.enterprise.model.Debtor;
import org.crystalrange.enterprise.model.Material;
import org.crystalrange.enterprise.model.SalesOrder;
import org.crystalrange.enterprise.model.SalesOrderItem;
import org.crystalrange.enterprise.service.SalesOrderService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;

public class SalesOrderController extends BaseController {

    protected SalesOrderService salesOrderService;
    protected SalesOrder salesOrder;

    public SalesOrderService getSalesOrderService() {
        return this.salesOrderService;
    }

    public void setSalesOrderService(SalesOrderService salesOrderService) {
        this.salesOrderService = salesOrderService;
    }

    public SalesOrder getSalesOrder() {
        return this.salesOrder;
    }

    public void setSalesOrder(SalesOrder salesOrder) {
        this.salesOrder = salesOrder;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.salesOrder = this.salesOrderService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Sales Order");
        } else {
            this.salesOrder = this.salesOrderService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Sales Order");
        }

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemSalesOrderSave(Event event) throws Exception {
        this.salesOrderSave();
    }

    public void onClick$toolbarbuttonSalesOrderSave(Event event) throws Exception {
        this.salesOrderSave();
    }

    private void salesOrderSave() throws Exception {
        Debtor debtor = new Debtor();
        debtor.setId(1l);
        
        Material material1 = new Material();
        material1.setId(1l);
        
        Material material2 = new Material();
        material2.setId(2l);
        
        SalesOrderItem salesOrderItem1 = new SalesOrderItem();
        salesOrderItem1.setMaterial(material1);
        salesOrderItem1.setPrice(10.00d);
        salesOrderItem1.setQuantity(100.00d);
        salesOrderItem1.setSalesOrder(this.salesOrder);
        
        SalesOrderItem salesOrderItem2 = new SalesOrderItem();
        salesOrderItem2.setMaterial(material2);
        salesOrderItem2.setPrice(20.00d);
        salesOrderItem2.setQuantity(200.00d);
        salesOrderItem2.setSalesOrder(this.salesOrder);
        
        this.salesOrder.setDebtor(debtor);
        this.salesOrder.getSalesOrderItems().add(salesOrderItem1);
        this.salesOrder.getSalesOrderItems().add(salesOrderItem2);
        
        this.salesOrderService.save(this.salesOrder,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }
}
